package com.zzyl.nursing.service.impl;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.zzyl.common.exception.base.BaseException;
import com.zzyl.common.utils.StringUtils;
import com.zzyl.nursing.service.WechatService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * TODO
 *
 * @Date 2025/10/22 15:49
 */
@Service
public class WechatServiceImpl implements WechatService {

    //小程序ID
    @Value("${weixin.appId}")
    private String appId;

    //小程序密钥
    @Value("${weixin.appSecret}")
    private String appSecret;

    //获取手机号API地址
    private static final String POST_PHONE_API_URL = "https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=";

    //小程序登录API地址
    private static final String GET_OPEN_ID_URL = "https://api.weixin.qq.com/sns/jscode2session";

    //获取访问Token的API地址
    private static final String GET_ACCESS_TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token";

    /*
     * 获取小程序配置信息
     */

    public Map<String, Object> getAppConfig() {
        Map<String, Object> appConfig = new HashMap<>();
        appConfig.put("appId", appId);
        appConfig.put("secret", appSecret);
        return appConfig;
    }

    /*
     * 获取小程序访问Token
     */

    public String getAccessToken() {
        Map<String, Object> paramMap = this.getAppConfig();
        paramMap.put("grant_type", "client_credential");

        String result = HttpUtil.get(GET_ACCESS_TOKEN_URL, paramMap);
        if (StringUtils.isBlank(result)) {
            throw new BaseException("获取小程序访问Token失败");
        }
        JSONObject jsonObject = JSONObject.parseObject(result);
        return jsonObject.getString("access_token");
    }

    /*
     * 根据临时凭证码
     *
     * @param code 微信小程序登录返回的openid
     * @return openId*/

    @Override
    public String getOpenId(String code) {
        Map<String, Object> paramMap = this.getAppConfig();
        paramMap.put("grant_type", "authorization_code");
        paramMap.put("js_code", code);

        String result = HttpUtil.get(GET_OPEN_ID_URL, paramMap);
        if (StringUtils.isBlank(result)) {
            throw new BaseException("获取openId失败");
        }
        JSONObject jsonObject = JSONObject.parseObject(result);
        return jsonObject.getString("openid");
    }

    /*
     * 获取微信小程序手机号
     *
     * @param code*/

    @Override
    public String getPhone(String code) {
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("code", code);
        String accessToken = this.getAccessToken();

        String result = HttpUtil.post(POST_PHONE_API_URL + accessToken, JSON.toJSONString(paramMap));
        if (StringUtils.isBlank(result)) {
            throw new BaseException("获取手机号失败");
        }
        JSONObject jsonObject = JSONObject.parseObject(result);
        String phoneInfoJSON = jsonObject.getString("phone_info");
        if (StringUtils.isNotBlank(phoneInfoJSON)) {
            JSONObject phoneObject = JSONObject.parseObject(phoneInfoJSON);
            return phoneObject.getString("phoneNumber");
        }
        return null;
    }

}